package cn.neusoft.luofa603.yuejian_29.activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.adapter.TrainSearchAdapter;
import cn.neusoft.luofa603.yuejian_29.bean.Training;
import cn.neusoft.luofa603.yuejian_29.model.TrainModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TrainSearchActivity extends AppCompatActivity {
    private ImageView bacck,train_search;
    private List<String> list = new ArrayList<String>();//创建一个String类型的数组列表。
    private TextView myTextView;
    private EditText editText;
    private Spinner mySpinner;
    private int train_type,result;
    private RecyclerView recyclerView;
    private String degree,SessionID,search_key;
    private ArrayAdapter<String> adapter;//创建一个数组适配器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trainsearch);
        initViews();
        spinner();
        initEvents();
        initData();
        setData();

    }
    public void initViews(){
        bacck=(ImageView)findViewById(R.id.back);
        editText=(EditText)findViewById(R.id.editText);
        train_search=(ImageView)findViewById(R.id.train_search);
        recyclerView = (RecyclerView) findViewById(R.id.rv_training_search);
        recyclerView.setLayoutManager(new LinearLayoutManager(TrainSearchActivity.this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

    }
    public void spinner(){
        // 添加一个下拉列表项的list，这里添加的项就是下拉列表的菜单项，即数据源
        list.add("可选");
        list.add("入门");
        list.add("菜鸟");
        list.add("进阶");
//        list.add("深圳");
       // myTextView = (TextView) findViewById(R.id.tview);//作用在创建点击事件时的文本说明。
        mySpinner = (Spinner) findViewById(R.id.spinner);
        //1.为下拉列表定义一个数组适配器，这个数组适配器就用到里前面定义的list。装的都是list所添加的内容
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);//样式为原安卓里面有的android.R.layout.simple_spinner_item，让这个数组适配器装list内容。
        //2.为适配器设置下拉菜单样式。adapter.setDropDownViewResource
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //3.以上声明完毕后，建立适配器,有关于sipnner这个控件的建立。用到myspinner
        mySpinner.setAdapter(adapter);
        //4.为下拉列表设置各种点击事件，以响应菜单中的文本item被选中了，用setOnItemSelectedListener
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {//选择item的选择点击监听事件
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                // TODO Auto-generated method stub
                // 将所选mySpinner 的值带入myTextView 中
               // myTextView.setText(adapter.getItem(arg2));//文本说明
                if ("入门"==adapter.getItem(arg2)){
                    train_type=1;
                }else if ("菜鸟"==adapter.getItem(arg2)){
                    train_type=2;
                }else if("进阶"==adapter.getItem(arg2)) {
                    train_type=3;
                }else {
                    train_type=0;
                }

                degree = Integer.toString(train_type);

            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                myTextView.setText("Nothing");
            }
        });

    }

    public void initEvents(){
        bacck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        train_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (train_type==0){
                    train_key_search();
                }else {
                    train_search();
                }
            }
        });

    }
    public void initData(){
          getSessionID();
    }
    public void getSessionID(){
        SharedPreferences sp= getSharedPreferences("sessionid", Context.MODE_PRIVATE);
        SessionID=sp.getString("sessionid", null);
    }
    public void setData(){
        search_key=editText.getText().toString();
        //Toast.makeText(TrainSearchActivity.this,"购买成功！",Toast.LENGTH_LONG).show();
    }
    public void train_search(){
        TrainModel trainModel = new TrainModel();
        Call<List<Training>> call = trainModel.train_search("train",SessionID,degree,search_key);
        call.enqueue(new Callback<List<Training>>() {
            @Override
            public void onResponse(Call<List<Training>> call, Response<List<Training>> response) {
                final List<Training> mDataList = response.body();
                TrainSearchAdapter trainSearchAdapter = new TrainSearchAdapter(TrainSearchActivity.this,mDataList);
                recyclerView.setAdapter(trainSearchAdapter);
                trainSearchAdapter.setItemClickListener(new TrainSearchAdapter.MyItemClickListener(){
                    public void onItemClick(View view, int position) {
                        Intent intent = new Intent(TrainSearchActivity.this,TrainInfoActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("Training",mDataList.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });
            }
            @Override
            public void onFailure(Call<List<Training>> call, Throwable t) {

            }
        });
    }
    public void train_key_search(){
        TrainModel trainModel = new TrainModel();
        Call<List<Training>> call = trainModel.train_key_search("train",SessionID,search_key);
        call.enqueue(new Callback<List<Training>>() {
            @Override
            public void onResponse(Call<List<Training>> call, Response<List<Training>> response) {
                final List<Training> mDataList = response.body();
                TrainSearchAdapter trainSearchAdapter = new TrainSearchAdapter(TrainSearchActivity.this,mDataList);
                recyclerView.setAdapter(trainSearchAdapter);
                trainSearchAdapter.setItemClickListener(new TrainSearchAdapter.MyItemClickListener(){
                    public void onItemClick(View view, int position) {
                        Intent intent = new Intent(TrainSearchActivity.this,TrainInfoActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("Training",mDataList.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });

            }

            @Override
            public void onFailure(Call<List<Training>> call, Throwable t) {

            }
        });
    }

}
